--- 高亮控件 --- 

local japi = require 'jass.japi'

local highlight = {}
setmetatable(highlight,highlight)

--- 存放所有的高亮控件
highlight.allhighlights = {}


local lights = {}

--- 模板
local template = [[
    Frame "HIGHLIGHT" "@name@" {
        HighlightAlphaMode "%s",    
        HighlightType "%s",     
        HighlightAlphaFile "%s",    
    }
]]

--- 构建
local function toc(self)
    local name = ("highlight_%s_%s_%s"):format(self.image:gsub('[\\%.%-%+]','_'),self.style,self.mode)
    if not lights[name] then 
        load_fdf(template:format(
            self.mode,
            self.style,
            self.image
        ):gsub("@name@",name))
        lights[name] = true 
    end 
    return name
end

local mt = extends(class.ui_base){}

--类型
mt.type = 'highlight'

--控件类型
mt._type = 'HIGHLIGHT'

--识别名
mt._name = "高亮"

--混合模式 // BLEND | ADD
mt.mode = 'ADD'

--高亮类型 // FILETEXTURE | SHADE | OUTLINE
mt.style = 'FILETEXTURE'

--高亮贴图
mt.image = [[UI\Widgets\BattleNet\bnet-button01-highlight-mouse.blp]]

--- 创建控件
function mt.new(parent, image, x, y, w, h)
    local control = class.highlight:builder
    {
        parent = parent,
        image = image,
        w = w,
        h = h,
        x = x,
        y = y,
    }
    return control
end 

--- 输出
function mt:__tostring()
    return string.format('[高亮]->%d',self.handle or 0)
end

--- 初始化
function mt:init()
    if self.handle then return end
    local handle = japi.CreateFrameByTagName( self._type, self._name, self.parent.handle, toc(self), self._index )
    if not handle or handle == 0 then
        class.ui_base.remove(self)
        log.error('创建highlight失败')
        return
    end
    self.handle = handle

    highlight.allhighlights[handle] = self
    class.ui_base.allframes[handle] = self
    self:set_width(self.w)
    self:set_height(self.h)
    self:set_position(self.x,self.y)
    if self.show_flag then
        self.show_flag = false
        self:show()
    else 
        self.show_flag = true
        self:hide()
    end
    return self
end 

---删除控件
function mt:remove()
    if self.removed then return end
    if self:is_simple() then return end
    highlight.allhighlights[self.handle] = nil
    class.ui_base.remove(self)
end
class.highlight = mt

return mt